package com.zoo.diannaopeijian.controller;

import com.github.pagehelper.PageInfo;
import com.zoo.diannaopeijian.comm.page.Page;
import com.zoo.diannaopeijian.domain.Ware;
import com.zoo.diannaopeijian.service.WareService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zx583 on 2018-01-10.
 */
@Controller
@RequestMapping(value = "/Ware*")
public class WareController extends BaseController {
    @Resource
    private WareService wareService;

    @RequestMapping(value = "WareManaPage")
    public String WareManaPageRoute() {
        return "ware/WareManaPage";
    }

    @RequestMapping(value = "WareAddPage")
    public String WareAddPageRoute() {
        return "ware/WareAddPage";
    }

    @RequestMapping(value = "WareUpdatePage")
    public String WareUpdatePageRoute() {
        return "ware/WareUpdatePage";
    }

    @RequestMapping(value = "WareComponentManaPage")
    public String WareComponentManaPageRoute() {
        return "ware/WareComponentManaPage";
    }



    @PostMapping(value = "WareGet")
    @ResponseBody
    public Map<String, Object> WareGet(@RequestParam(required = true, defaultValue = "1") Integer page,
                                       @RequestParam(required = true, defaultValue = "10") Integer rows,
                                       @RequestParam(required = true, defaultValue = "false") boolean isFuzzy,
                                       Ware ware
    ) {
        Map<String, Object> result = new HashMap<>();

        List<Ware> datas = isFuzzy ? wareService.getFuzzyWareWithPage(page, rows, ware) : wareService.getWareWithPage(page, rows, ware);

        result.put(rowsWord, datas);
        result.put(totalWord, new PageInfo<Ware>(datas).getTotal());

        return result;
    }

    @PostMapping(value = "WareList")
    @ResponseBody
    public List<Ware> WareList(String q) {
        new Page().onPage();

        return wareService.getWareList(new Ware(null, q, null, q));
    }

    @PostMapping(value = "WarePost")
    public @ResponseBody
    String WarePost(Ware ware) {
        return wareService.addWare(ware) ? success : fail;
    }

    @PostMapping(value = "WareDelete")
    @ResponseBody
    public String WareDelete(Integer wareid) {
        return wareService.deleteWare(wareid) ? success : fail;
    }

    @PostMapping(value = "WareUpdate")
    @ResponseBody
    public String WareUpdate(Ware ware) {
        return wareService.updateWare(ware) ? success : fail;
    }
}
